package com.chuzhiyun.chengguoxy.wxapi;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Cookie;
import okhttp3.HttpUrl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.chuzhiyun.chengguoxy.feature.bean.UserInfoBean;
import com.chuzhiyun.chengguoxy.feature.login.PnlessActivity;
import com.chuzhiyun.chengguoxy.feature.login.SmsCodeActivity;
import com.chuzhiyun.chengguoxy.global.Config;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cookie.store.CookieStore;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI wxAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        wxAPI = WXAPIFactory.createWXAPI(this, Config.wxAppId,true);
        wxAPI.registerApp(Config.wxAppId);
        wxAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        wxAPI.handleIntent(getIntent(),this);
    }

    @Override
    public void onReq(BaseReq arg0) {
        Log.i("ansen","WXEntryActivity onReq:"+arg0);
    }

    @Override
    public void onResp(BaseResp resp){
        if(resp.getType()== ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX){//分享
            System.out.println("share");
        }else if(resp.getType()==ConstantsAPI.COMMAND_SENDAUTH){//登陆
            SendAuth.Resp authResp = (SendAuth.Resp) resp;
            String wxCode = authResp.code;
            EventBus.getDefault().post(wxCode);
            //微信登录
            wxLoginClick(wxCode);
        }
        finish();
    }

    private void wxLoginClick(String wxCode){
        CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
        cookieStore.removeAllCookie();

        String url = NetApi.postWXLogin();
        HashMap<String,String>param = new HashMap<>();
        param.put("wxCode",wxCode);
        NetService.getInstance().postParam(url, param, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                if (statusCode==200){
                    Gson gson = new Gson();
                    Map dict = gson.fromJson(response,Map.class);
                    Boolean registered = (Boolean) dict.get("registered");
                    if (registered){
                        //已经注册
                        getUserInfo();
                    }
                    else {
                        //未注册
                        Intent intent = new Intent();
                        intent.setClass(WXEntryActivity.this, PnlessActivity.class);
                        intent.putExtra("wxCode",wxCode);
                        startActivity(intent);
                    }
                }


            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }

    private void getUserInfo(){
        String url = NetApi.getStudentInfo();
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                if (statusCode==200){
                    Gson gson = new Gson();
                    UserInfoBean bean = gson.fromJson(response,UserInfoBean.class);
                    System.out.println(bean);
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }
}
